package com.wcx.application.controller;

import com.wcx.application.config.MyCustomAnnotation;
import com.wcx.application.domian.AdminBean;
import com.wcx.application.service.AdminService;
import com.wcx.application.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Value("${web.upload-path}")
    private String uploadPath;


    @GetMapping("/list")
    public Result getList() {
        List<AdminBean> list = (List<AdminBean>) redisTemplate.opsForValue().get("adminList");
        if (list == null || list.isEmpty()) {
            // 如果为空，从服务中获取新列表
            list = adminService.getList();

            // 将新列表存储到Redis
            redisTemplate.opsForValue().set("adminList", list);
        }
        System.out.println("redis请求");
        // 返回带有管理员列表的结果
        return Result.ok().data("list", list);

    }
    @GetMapping("/listnoredis")
    public Result getListnoredis() {
        System.out.println("无redis请求");

        return Result.ok().data("list", adminService.getList());
    }

    @PostMapping("/update")
    public Result update(@RequestBody AdminBean adminBean) {
            int i = adminService.update(adminBean);
            if (i != 0) {
                redisTemplate.delete("adminList");
                return Result.ok();
            } else {
                return Result.error();
            }
    }
    @PostMapping("/search")
    public Result search(@RequestBody AdminBean adminBean) {
        System.out.println("---"+adminBean.getGender());
        return Result.ok().data("list", adminService.searchList(adminBean));
    }
    @PostMapping("/create")
    public Result create(@RequestBody AdminBean adminBean) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY年MM月dd日 HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            String time = simpleDateFormat.format(calendar.getTime());
            adminBean.setCreateTime(time);
            if (adminBean.getAvatar() == "") {
                adminBean.setAvatar("917488dc_E380397_8f92a53e.png");
            }
            int i = adminService.create(adminBean);
            if (i != 0) {
                return Result.ok();
            } else {
                return Result.error();
            }

    }

    @DeleteMapping("/delete")
    public Result delete(int id) {
        int i = adminService.delete(id);
        System.out.println("12345");

        if (i != 0) {
            redisTemplate.delete("adminList");
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile photo, @RequestParam("id") int id, HttpServletRequest request) {
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());

        int i = adminService.updateAvatarById(photo.getOriginalFilename(), id);
        saveFile(photo, uploadPath);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    @PostMapping("/uploadA")
    public Result uploadA(@RequestParam("file") MultipartFile photo, String username, HttpServletRequest request) {
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());

        int i = adminService.updateAvatarByUserName(photo.getOriginalFilename(), username);
        saveFile(photo, uploadPath);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    private void saveFile(MultipartFile photo, String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
        File file1 = new File(path + photo.getOriginalFilename());
        try {
            photo.transferTo(file1);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}